<template>
  <div class="container">
    <!--搜索-->
    <div v-show="seek">
      <div class="handle-box">
        <el-input v-model="select_word" placeholder="筛选关键词" class="handle-input mr10"
                  @keyup.enter.native="search">
          <i slot="prefix" class="el-input__icon el-icon-search"></i>
        </el-input>
        <el-button type="primary" icon="search" @click="search">搜索</el-button>
        <el-button type="success" size="small" @click="handleAdd">新增</el-button>
      </div>
      <!--表格-->
      <el-table onselectstart="return false" :data="tableData" border style="width: 100%;margin-top: 20px">
        <el-table-column label="医院名称" prop="showName"></el-table-column>
        <el-table-column label="评级" prop="grade"></el-table-column>
        <el-table-column label="等级" prop="level"></el-table-column>
        <el-table-column label="省" prop="province"></el-table-column>
        <el-table-column label="市" prop="city"></el-table-column>
        <el-table-column label="操作" width="230">
          <template slot-scope="scope">
            <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--新增-->
    <div v-show="Added">
      <h3>基本信息</h3>
      <el-row :gutter="20">
        <el-col :span="12">
          <div class="interval">
            <span class="spn">医院名称:</span>
            <el-input class="ipt" v-model="form.hospitalName" placeholder="请输入医院名称"></el-input>
            <el-checkbox v-model="checked" @change="displayName">是否为显示名字</el-checkbox>
          </div>
          <div class="interval">
            <span class="spn">显示名字:</span>
            <el-input class="ipt" :disabled="disabledInput" v-model="form.showName"
                      placeholder="请输入显示名字"></el-input>
          </div>
          <div class="interval">
            <span class="spn" style="height: 100px;">医院别名:</span>
            <el-input type="textarea" :rows="5" class="ipt" v-model="form.nickName"
                      placeholder="医院别名请用英文逗号分隔"></el-input>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="interval">
            <span class="spn">评级:</span>
            <el-select class="ipt" v-model="form.level" @change='grade' placeholder="请输入医院评级">
              <el-option
                v-for="item in grades"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </div>
          <div class="interval">
            <span class="spn">等级:</span>
            <el-select class="ipt" v-model="form.grade" @change='grade' placeholder="请输入医院等级">
              <el-option
                v-for="item in levels"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </div>
          <div class="interval">
            <span class="spn">等级全称:</span>
            <el-input class="ipt" disabled v-model="form.hospitalLevelFull" placeholder="医院等级全称"></el-input>
          </div>
          <div class="interval">
            <span class="spn">医院分类:</span>
            <el-select class="ipt" v-model="form.hospitalClassify" @change='grade' placeholder="请输入医院分类">
              <el-option
                v-for="item in hospitalClassify"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </div>
        </el-col>
      </el-row>
      <h3>医院地址信息</h3>
      <el-row :gutter="20">
        <el-col :span="12">
          <div class="interval">
            <span class="spn">医院所在省:</span>
            <el-select class="ipt" v-model="sheng" value-key="value" @change='Provinceer' placeholder="请输入医院所在省">
              <el-option
                v-for="item in provinces"
                :key="item.value"
                :label="item.label"
                :value="item">
              </el-option>
            </el-select>
          </div>
          <div class="interval">
            <span class="spn">医院所在市:</span>
            <el-select class="ipt" v-model="shi" @change='cityer' placeholder="请输入医院所在市">
              <el-option
                v-for="item in citys"
                :key="item.value"
                :label="item.label"
                :value="item">
              </el-option>
            </el-select>
          </div>
          <div class="interval">
            <span class="spn">医院所在区:</span>
            <el-select class="ipt" v-model="qu" placeholder="请输入医院所在区">
              <el-option
                v-for="item in areas"
                :key="item.value"
                :label="item.label"
                :value="item">
              </el-option>
            </el-select>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="interval">
            <span class="spn">详细地址:</span>
            <el-input class="ipt" v-model="form.hospitalAddress" placeholder="请输入详细地址"></el-input>
          </div>
          <div class="interval">
            <span class="spn">医院网址:</span>
            <el-input class="ipt" v-model="form.website" placeholder="请输入医院网址"></el-input>
          </div>
          <div class="interval">
            <span class="spn">电话:</span>
            <el-input class="ipt" v-model="form.phoneNumber" placeholder="请输入电话"></el-input>
          </div>
        </el-col>
      </el-row>
      <h3>医院详细信息</h3>
      <el-row :gutter="20">
        <el-col :span="12">
          <div class="interval">
            <span class="spn">院长:</span>
            <el-input class="ipt" v-model="form.hospitalDean" placeholder="请输入院长"></el-input>
          </div>
          <div class="interval">
            <span class="spn">门诊量:</span>
            <el-input class="ipt" v-model="form.outpatientVolume" placeholder="请输入门诊量"></el-input>
          </div>
          <div class="interval">
            <span class="spn">床位数:</span>
            <el-input class="ipt" v-model="form.bedsNumber" placeholder="请输入床位数"></el-input>
          </div>
          <div class="interval">
            <span class="spn">是否激活:</span>
            <el-select class="ipt" v-model="form.isActive" placeholder="请输入">
              <el-option
                v-for="item in Active"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="interval">
            <span class="spn">科室数:</span>
            <el-input class="ipt" v-model="form.departmentsNumber" placeholder="请输入员工数"></el-input>
          </div>
          <div class="interval">
            <span class="spn">员工数:</span>
            <el-input class="ipt" v-model="form.staffNumber" placeholder="请输入员工数"></el-input>
          </div>
          <div class="interval">
            <span class="spn">建院年份:</span>
            <el-select class="ipt" v-model="form.yearEstablish" placeholder="请输入">
              <el-option
                v-for="item in yearEstablishs"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </div>
          <div class="interval">
            <span class="spn">是否医保:</span>
            <el-select class="ipt" v-model="form.isMedicalInsurance" placeholder="请输入">
              <el-option
                v-for="item in isMedicalInsurances"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </div>
        </el-col>
        <el-col :span="24">
          <div class="interval">
            <span class="spn" style="height: 100px;">简介:</span>
            <el-input type="textarea" :rows="5" class="ipt" style="width: 83%" v-model="form.summary"
                      placeholder="请输入内容"></el-input>
          </div>
        </el-col>
      </el-row>
      <div class="but">
        <el-button type="primary" v-show="yesShow" @click="yes">确定</el-button>
        <el-button type="primary" v-show="yes1Show" @click="yes1">确定</el-button>
        <el-button type="danger" @click="abolish">取消</el-button>
      </div>
    </div>
    <!-- 删除提示框 -->
    <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
      <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
      <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
    </el-dialog>
    <!--没有搜索到数据-->
    <el-dialog
      :visible.sync="centerDialogVisible"
      width="30%"
      center>
      <div style="text-align: center">
        <span
          style='display: inline-block;background: url("../../../static/img/weep.png") no-repeat;background-size:100%;width: 50px;height: 50px;vertical-align: middle;'></span>
        <span style="display:inline-block;font-size: 20px;font-weight: bold">没有搜索到数据!</span>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        /*搜索*/
        select_word: '',
        seek: true,
        /*新增*/
        Added: false,
        yesShow: false,
        yes1Show: false,
        /*删除*/
        delVisible: false,
        /*URL*/
        URL: this.$com.Url,
        /*搜索数据*/
        tableData: [],
        /*评级*/
        grades: [
          {
            value: '一级',
            label: '一级'
          }, {
            value: '二级',
            label: '二级'
          }, {
            value: '三级',
            label: '三级'
          }, {
            value: '无',
            label: '无'
          }],
        /*等级*/
        levels: [
          {
            value: '甲等',
            label: '甲等'
          }, {
            value: '乙等',
            label: '乙等'
          }, {
            value: '丙等',
            label: '丙等'
          }, {
            value: '无',
            label: '无'
          }],
        /*省*/
        provinces: [],
        sheng: '',
        /*市*/
        citys: [],
        shi: '',
        /*区*/
        areas: [],
        qu: '',
        /*是否激活*/
        Active: [
          {
            value: 1,
            label: '是'
          }, {
            value: 0,
            label: '否'
          }],
        /*是否医保*/
        isMedicalInsurances: [
          {
            value: '无',
            label: '无'
          },
          {
            value: '医保',
            label: '医保'
          }, {
            value: '非医保',
            label: '非医保'
          }],
        /*医院分类*/
        hospitalClassify: [
          {
            value: '专科医院',
            label: '专科医院'
          }, {
            value: '综合医院',
            label: '综合医院',
          }
          , {
            value: '中医医院',
            label: '中医医院',
          }
          , {
            value: '儿科医院',
            label: '儿科医院',
          }
          , {
            value: '社区医院',
            label: '社区医院',
          }
        ],
        /*建院年份*/
        yearEstablishs: [],
        /*医院名称是否为显示名称*/
        checked: false,
        disabledInput: false,
        form: {
          hospitalName: "",
          showName: "",
          nickName: "",
          grade: "",
          gradeId: "",
          level: "",
          levelId: "",
          hospitalLevelFull: "",
          hospitalClassify: "",
          province: "",
          provinceId: "",
          area: "",
          website: "",
          city: "",
          hospitalAddress: "",
          phoneNumber: "",
          hospitalDean: "",
          outpatientVolume: "",
          bedsNumber: "",
          isActive: 1,
          departmentsNumber: "",
          staffNumber: "",
          yearEstablish: "",
          isMedicalInsurance: "无",
          summary: "",
        },
        data: {},
        /*无数据*/
        centerDialogVisible: false,
      }
    },
    created() {
      this.province(this.URL, '/region/AllProvince');
      this.yearEstablish()
    },
    methods: {
      /*搜索*/
      search() {
        if (this.select_word.length > 0) {
          this.$axios({
            method: 'GET',
            url: this.URL + '/huimei/hco/hco_query/' + this.select_word,
          }).then((res) => {
            if (res.data.data.result.length > 0) {
              this.tableData = res.data.data.result;
              this.$message.success('搜索成功！');
            } else {
              this.tableData = [];
              this.centerDialogVisible = true;
              setTimeout(() => {
                this.centerDialogVisible = false;
              }, 1500);
              /*              this.$message.warning('无数据！');*/
            }
          })
        } else {
          this.$message.warning('不能为空！');
        }
      },
      /*新增*/
      handleAdd() {
        this.seek = false;
        this.Added = true;
        this.yesShow = true;
      },
      /*取消*/
      abolish() {
        this.seek = true;
        this.Added = false;
        this.yesShow = false;
        this.yes1Show = false;
        this.$router.go(0)
      },
      /*编辑*/
      handleEdit(index, row) {
        this.seek = false;
        this.Added = true;
        this.yes1Show = true;
        this.form.hospitalName = row.hospitalName;
        this.form.showName = row.hospitalName;
        this.form.nickName = row.nickName;
        this.form.grade = row.grade;
        this.form.gradeId = row.gradeId;
        this.form.level = row.level;
        this.form.levelId = row.levelId;
        this.form.hospitalLevelFull = row.hospitalLevelFull;
        this.form.hospitalClassify = row.hospitalClassify;
        this.sheng = row.province;
        this.form.province = row.province;
        this.form.provinceId = row.provinceId;
        this.shi = row.city;
        this.form.city = row.city;
        this.qu = row.area;
        this.form.area = row.area;
        this.form.hospitalAddress = row.hospitalAddress;
        this.form.website = row.website;
        this.form.phoneNumber = row.phoneNumber;
        this.form.hospitalDean = row.hospitalDean;
        this.form.outpatientVolume = row.outpatientVolume;
        this.form.bedsNumber = row.bedsNumber;
        this.form.isActive = Number(row.isActive);
        this.form.departmentsNumber = row.departmentsNumber;
        this.form.staffNumber = row.staffNumber;
        this.form.yearEstablish = row.yearEstablish;
        this.form.isMedicalInsurance = row.isMedicalInsurance;
        this.form.summary = row.summary;
        this.form.id = row.id;
        this.form.hospitalUid = row.hospitalUid;
      },
      /*删除处理*/
      handleDelete(index, row) {
        this.data.id = row.id;
        this.data.isActive = '0';
        this.data.hospitalUid = row.hospitalUid;
        this.idx = index;
        this.delVisible = true;
      },
      /*确定删除*/
      deleteRow() {
        this.tableData.splice(this.idx, 1);
        this.$axios({
          method: 'POST',
          headers: {'Content-Type': 'application/json;charset=UTF-8'},
          url: this.URL + '/huimei/hco/updateState',
          data: JSON.stringify(this.data),
        }).then((res) => {
          if (res.data.code == 200) {
            this.delVisible = false;
            this.$message.success('删除成功');
          }
        })
      },
      /*建院年份*/
      yearEstablish() {
        let data = 2030;
        for (let i = 1949; i <= data; i++) {
          let obj = {
            value: i,
            label: i
          };
          this.yearEstablishs.push(obj)
        }
      },
      /*医院名称是否为显示名称*/
      displayName() {
        if (this.checked == true) {
          this.disabledInput = true;
          this.form.showName = this.form.hospitalName;
        } else {
          this.disabledInput = false;
          this.form.showName = '';
        }
      },
      /*等级全称*/
      grade() {
        this.form.hospitalLevelFull = this.form.level + this.form.grade;
        if (this.form.level == '一级') {
          this.form.levelId = '1'
        } else if (this.form.level == '二级') {
          this.form.levelId = '2'
        } else if (this.form.level == '三级') {
          this.form.levelId = '3'
        } else if (this.form.level == '无') {
          this.form.levelId = '0'
          this.form.hospitalLevelFull = this.form.grade;
        }
        if (this.form.grade == '甲等') {
          this.form.gradeId = '1';
        } else if (this.form.grade == '乙等') {
          this.form.gradeId = '2';
        } else if (this.form.grade == '丙等') {
          this.form.gradeId = '3';
        } else if (this.form.grade == '无') {
          this.form.gradeId = '0';
          this.form.hospitalLevelFull = this.form.level;
        }
      },
      /*新增确定*/
      yes() {
        if (this.form.hospitalName == '') {
          this.$message.error('医院名称不能为空！');
        } else if (this.form.showName == '') {
          this.$message.error('显示名字不能为空！');
        } else if (this.form.nickName == '') {
          this.$message.error('医院别名不能为空！');
        } else if (this.form.grade == '') {
          this.$message.error('医院评级不能为空！');
        } else if (this.form.level == '') {
          this.$message.error('医院等级不能为空！');
        } else if (this.form.hospitalClassify == '') {
          this.$message.error('医院分类不能为空！');
        } else if (this.sheng == '') {
          this.$message.error('医院所在省不能为空！');
        } else {
          if (this.sheng != '') {
            this.form.province = this.sheng.label;
            this.form.provinceId = this.sheng.value.toString();
          }
          if (this.shi != '') {
            this.form.city = this.shi.label;
          }
          if (this.qu != '') {
            this.form.area = this.qu.label
          }
          if (this.form.outpatientVolume == '') {
            this.form.outpatientVolume = 0
          }
          if (this.form.bedsNumber == '') {
            this.form.bedsNumber = 0
          }
          if (this.form.departmentsNumber == '') {
            this.form.departmentsNumber = 0
          }
          if (this.form.staffNumber == '') {
            this.form.staffNumber = 0
          }
          if (this.form.yearEstablish == '') {
            this.form.yearEstablish = 0
          }
          if (this.form.isActive == 0) {
            this.form.isActive = '0'
          }
          if (this.form.isActive == 1) {
            this.form.isActive = '1'
          }
          this.POST(this.URL, '/huimei/hco/add', '添加')
        }
      },
      /*编辑确定*/
      yes1() {
        this.POST(this.URL, '/huimei/hco/update', '修改');
      },
      /*获取省*/
      province(Url, Port) {
        this.$axios({
          method: 'GET',
          url: Url + Port,
        }).then((res) => {
          let _this = this;
          for (let data of res.data.data) {
            let obj = {
              value: data.id,
              label: data.name
            };
            _this.provinces.push(obj)
          }
        })
      },
      Provinceer() {
        this.citys = [];
        this.shi = '';
        this.qu = '';
        this.city(this.URL, '/region/AllCity/' + this.sheng.value);
      },
      /*获取市*/
      city(Url, Port) {
        this.$axios({
          method: 'GET',
          url: Url + Port,
        }).then((res) => {
          let _this = this;
          for (let data of res.data.data) {
            let obj = {
              value: data.id,
              label: data.name
            };
            _this.citys.push(obj)
          }
        })
      },
      cityer() {
        this.areas = [];
        this.qu = '';
        this.area(this.URL, '/region/AllCity/' + this.shi.value);
      },
      /*获取区*/
      area(Url, Port) {
        this.$axios({
          method: 'GET',
          url: Url + Port,
        }).then((res) => {
          let _this = this;
          for (let data of res.data.data) {
            let obj = {
              value: data.id,
              label: data.name
            };
            _this.areas.push(obj)
          }
        })
      },
      POST(Url, Port, message) {
        this.$axios({
          method: 'POST',
          headers: {'Content-Type': 'application/json;charset=UTF-8'},
          url: Url + Port,
          data: JSON.stringify(this.form),
        }).then((res) => {
          if (res.data.code == 200) {
            this.$message({
              message: '恭喜，' + message + '成功!',
              type: 'success'
            });
            setTimeout(() => {
              this.$router.go(0)
            }, 10)
          }
        })

      },
      /*去除非法字符*/
      excludeSpecial(s) {
        s = s.replace(/[^\u4e00-\u9fa5a-zA-Z0-9\w]/g, ' ');
        return s;
      }
    },
  }
</script>

<style scoped>
  .container {
    padding: 30px;
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 5px;
  }

  .interval {
    padding-bottom: 10px;
  }

  .spn {
    display: inline-block;
    width: 90px;
    font-weight: bold;
    color: #909399;
    font-size: 16px;
    text-align: right;
  }

  .ipt {
    display: inline-block;
    width: 280px;
  }

  .but {
    padding-top: 20px;
    text-align: center;
  }

  .handle-input {
    width: 300px;
    display: inline-block;
  }

  .del-dialog-cnt {
    font-size: 16px;
    text-align: center
  }
</style>
